<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <base href="${base}/">
    <title>erupt-log</title>
    <link href="log/xterm.css" rel="stylesheet">
    <style>
        html {
            height: 100%;
        }

        body {
            margin: 0;
            min-height: 700px;
        }

        @font-face {
            font-family: 'Courier New';
            src: url('log/Courier-New.ttf');
        }
    </style>
</head>
<body>
<div style="background: #1e1f22;padding: 0 12px;position: fixed;left: 0;right: 0;top: 0;bottom: 0">
    <div id="terminal" style="width:100%;height:100%">

    </div>
</div>
<script src="log/axios.min.js"></script>
<script src="log/xterm.js"></script>
<script src="log/xterm-addon-fit.js"></script>
<script>
    let cloudNode = new URLSearchParams(location.search).get("_erupt") === 'CloudNode';
    if (cloudNode) {
        if (window.self !== window.top) {
            let interval;

            function removeHeader() {
                try {
                    // window.parent.document.getElementsByClassName("ant-modal-header")[0].remove();
                    // window.parent.document.getElementsByClassName("ant-modal-close-x")[0].style.color = "#fff";
                    window.parent.document.getElementsByClassName("ant-drawer-header")[0].remove();
                } catch (e) {
                    console.warn(e);
                    interval && clearInterval(interval);
                }
            }

            removeHeader()
            interval = setInterval(() => {
                removeHeader()
            }, 100)
        }
    }
    const xterm = new Terminal({
        convertEol: true,
        fontFamily: `Courier New, 'Fira Mono', monospace`,
        fontSize: 13,
        lineHeight: 1.4,
        cursorBlink: true,
        disableStdin: true,
        theme: {
            background: "#1e1f22", //背景色
        }
    });
    var fitAddon = new FitAddon.FitAddon();
    xterm.loadAddon(fitAddon)
    xterm.open(document.getElementById('terminal'));
    let offset = null;

    function getLog() {
        let url = cloudNode ? 'erupt-cloud-api/erupt-cloud-node-log' : "erupt-api/tool/erupt-log"
        axios.get(url, {
            headers: {
                token: parent.getAppToken().token,
            },
            params: {
                size: 500,
                offset: offset,
                nodeName: cloudNode && '${rows[0].nodeName}'
            }
        }).then((res) => {
            for (let key in res.data) {
                xterm.writeln(res.data[key].content);
                offset = res.data[key].num;
            }
            if (res.data.length > 0) {
                fitAddon.fit()
            }
        }, error => {
            if (error.response) {
                if (error.response.status === 401) {
                    window.parent.location.reload();
                } else if (error.response.status === 403) {
                    window.location.href = "${base}/error/403.html"
                } else {
                    xterm.writeln("status: " + error.response.status + (error.response.data && (" → " + error.response.data.message)));
                }
            } else {
                xterm.writeln(error.message);
                offset = null;
            }
        });
    }

    getLog();
    setInterval(function () {
        getLog();
    }, 1500)
    window.onresize = function () {
        fitAddon.fit()
    }
</script>
</body>
</html>